import tkinter as tk
from tkinter import *
import re
import requests
import json
import sys
# import os
import time

# 创建主体窗口程序
root = tk.Tk()
root.geometry("200x184+1080+500")
# root.geometry("200x220+1079+519")   # 右下角
root.attributes("-alpha",0.5)
root.configure(background='red')
root.overrideredirect(True)
root.wm_attributes('-topmost',True)

#创建Listbox控件
listbox1 = Listbox (root,width=10,xscrollcommand=1)
listbox2 = Listbox (root,width=10,xscrollcommand=1)
listbox3 = Listbox (root,width=10,xscrollcommand=1)
listbox1.pack(side=LEFT)
listbox3.pack(side=RIGHT)
listbox2.pack(side=RIGHT)

code_name = []
code_data = []
code_time = []
# 读入基金代码
result = []
with open(r'code.txt','r') as f:
    for line in f:
        result.append((list(line.strip('\n').split(','))))

'''jzrq净值日期 dwjz当日净值 gsz净值估算 gszzl估算涨跌百分比'''
def getInfo():  # 获取信息函数
    for i in result:
        code = ' '.join(i)
        # print("代号为：", code)
        url = "http://fundgz.1234567.com.cn/js/%s.js" % code
        # print(url)
        # 浏览器头
        headers = {'content-type': 'application/json',
                   'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
        r = requests.get(url, headers=headers)
        # 返回信息
        content = r.text
        # 正则表达式
        pattern = r'^jsonpgz\((.*)\)'
        # 查找结果
        search = re.findall(pattern, content)
        # print(search)
        # 遍历结果
        for j in search:
            data = json.loads(j)

            code_data.append(data['gsz'])
            gztime = data['gztime']
            GZtime=""
            for l in gztime:
                if l==" ":
                    GZtime=""
                else:
                    GZtime+=l
            # print(GZtime)
            code_time.append(GZtime)
            code_name.append(data['name'])
            # print("基金:{},收益值:{},时间：".format(data['name'], data['gsz']),GZtime,sep='')

def button_1(event):    #双击鼠标检测函数
    global x, y
    x, y = event.x, event.y
    # print("event.x, event.y = ", event.x, event.y)
    print("已退出")
    root.destroy()

def topWin():   # 窗体置顶函数
    tp = Toplevel(root)
    tp.attributes('-topmost', True)

def clear_data():   # 清空列表原始数据
    code_name.clear()
    code_data.clear()
    code_time.clear()

def f5():   # 递归刷新控件
    clear_data()
    print("开始获取信息")
    getInfo()
    print("成功获取信息")
    listbox3.delete(0, END)
    listbox2.delete(0, END)
    listbox1.delete(0, END)
    for i in range(len(code_name)):
        listbox1.insert(END, code_name[i])
    for i in range(len(code_data)):
        listbox2.insert(END, code_data[i])
    for i in range(len(code_time)):
        listbox3.insert(END, code_time[i])
    # while True:
    time.sleep(1)
    root.after(60000, f5)
    print("开始刷新")
    print('刷新成功')

root.bind("<Double-Button-1>", button_1)
# root.after(5000,f5)
f5()
root.mainloop()